package com.edu.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

// 可重入锁
public class SynLockTest {
    public static void main(String[] args) {
        // synchronized
        Object object = new Object();
        new Thread(() -> {
            synchronized (object) {
                System.out.println(Thread.currentThread().getName() + "外层");
                synchronized (object) {
                    System.out.println(Thread.currentThread().getName() + "内层");
                }
            }
        }, "t1").start();

        // Lock
        ReentrantLock lock = new ReentrantLock();
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "外层");
                lock.unlock();

                lock.lock();
                System.out.println(Thread.currentThread().getName() + "内层");
                lock.lock();
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "最内层");
            } finally {
                lock.unlock();
            }
        }, "t2").start();

        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName());
            lock.unlock();
        }, "t3").start();
    }
}
